package cn.com.surker.system.service.impl;

import cn.com.surker.common.Code;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.system.dao.SysMenuInfoDao;
import cn.com.surker.system.dao.SysRoleDao;
import cn.com.surker.system.dao.SysUserDao;
import cn.com.surker.system.entity.SysMenuInfo;
import cn.com.surker.system.entity.SysRole;
import cn.com.surker.system.entity.SysUser;
import cn.com.surker.system.service.RoleService;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author lucky
 * @create 2022年06月27 15:32
 * @desc
 */
@Service
public class RoleServiceImpl implements RoleService {


    @Resource
    private SysRoleDao roleDao;

    /**系统用户**/
    @Resource
    private SysUserDao userDao;

    /**系统菜单**/
    @Resource
    private SysMenuInfoDao menuInfoDao;

    /**
     * 查询角色用户
     * @param rid 角色ID
     * @param name 员工名称
     * @return
     */
    public Map<String, Object> queryRoleUser(Long rid, String name, Integer offset, Integer limit) {
        if (StringUtils.isNull(name))
            name = "";

        List<Map<String, Object>> list = roleDao.queryRoleUser(rid, name, offset, limit);
        SysUser user = new SysUser();
        user.setStatus(StateEnum.ENABLED.getCode());
        user.setName(name);
        Integer total = userDao.queryUserTotal(user);

        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        map.put("total", total);

        return map;
    }

    /**
     * 查询角色菜单
     * @param rid 角色ID
     * @param name 菜单名称
     * @param type 菜单类型(0:手机菜单,1:后台菜单)
     * @return
     */
    public Map<String, Object> queryRoleMenu(Long rid, String name, Integer type, Integer offset, Integer limit) {
        if (StringUtils.isNull(rid))
            return null;

        if (StringUtils.isNull(name))
            name = "";

        List<Map<String, Object>> list = roleDao.queryRoleMenu(rid, name, type, offset, limit);
        SysMenuInfo menuInfo = new SysMenuInfo();
        menuInfo.setStatus(StateEnum.ENABLED.getCode());
        menuInfo.setName(name);
        menuInfo.setType(type);
        Integer total = menuInfoDao.queryMenuTotal(menuInfo);

        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        map.put("total", total);

        return map;
    }

    @Override
    public Map<String, Object> queryRoleMenuV1(Long rid, String name, Integer type, Integer offset, Integer limit) {
        return null;
    }

    /**
     * 添加角色用户
     * @param uid 用户ID
     * @param rid 角色ID
     * @return
     */
    public Integer deleteRoleUser(Long uid, Long rid) {
        if (StringUtils.isAllParamNull(uid, rid))
            /**缺少必填参数**/
            return Code.PARAM_ERROR.getCode();

        Integer code = roleDao.updateRoleUser(uid, rid, StateEnum.DISABLED.getCode());
        if (code > 0)
            return 0;
        else
            return -1;
    }

	@Override
	public Integer deleteRoleUser(Long uid) {
    	if(uid == null){
    		return -1;
		}
		Integer code = roleDao.deleteAllRoleUser(uid, StateEnum.DISABLED.getCode());
		return code;
	}

	/**
     * 解除角色菜单
     * @param mid 菜单ID
     * @param rid 角色ID
     * @return
     */
    public Integer deleteRoleMenu(Long mid, Long rid) {
        if (StringUtils.isAllParamNull(mid, rid))
        /**缺少必填参数**/
            return Code.PARAM_ERROR.getCode();

        Integer code = roleDao.updateRoleMenu(mid, rid, StateEnum.DISABLED.getCode());
        if (code > 0)
            return 0;
        else
            return -1;
    }

    /**
     * 添加角色用户
     * @param uid 用户ID
     * @param rid 角色ID
     * @return
     */
    public Integer saveRoleUser(Long uid, Long rid) {
        if (StringUtils.isAllParamNull(uid, rid))
        /**缺少必填参数**/
            return Code.PARAM_ERROR.getCode();

        Integer count = roleDao.queryRoleUserCount(uid, rid);
        if (count > 0) {
            /**如果存在直接修改**/
            Integer code = roleDao.updateRoleUser(uid, rid, StateEnum.ENABLED.getCode());
            if (code > 0)
                return 0;
            else
                return -1;
        } else {
            Integer code = roleDao.saveRoleUser(uid, rid, StateEnum.ENABLED.getCode(), DateUtil.convertString(new Date()));
            if (code > 0)
                return 0;
            else
                return -1;
        }
    }

    /**
     * 添加角色菜单
     * @param mid 菜单ID
     * @param rid 角色ID
     * @return
     */
    public Integer saveRoleMenu(Long mid, Long rid) {
        if (StringUtils.isAllParamNull(mid, rid))
            /**缺少必填参数**/
            return Code.PARAM_ERROR.getCode();

        Integer count = roleDao.queryRoleMenuCount(mid, rid);
        if (count > 0) {
            /**如果存在直接修改**/
            Integer code = roleDao.updateRoleMenu(mid, rid, StateEnum.ENABLED.getCode());
            if (code > 0)
                return 0;
            else
                return -1;
        } else {
            Integer code = roleDao.saveRoleMenu(mid, rid, StateEnum.ENABLED.getCode(), DateUtil.convertString(new Date()));
            if (code > 0)
                return 0;
            else
                return -1;
        }
    }

    /**
     * 分页查询数据
     * @param dto
     * @return
     */
    public IPage findAllByPage(SysRole dto, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<SysRole> query = new QueryWrapper();
        if (dto != null && !StringUtils.isNull(dto.getName()))
            query.like("name", dto.getName());

        IPage<SysRole> iPage = roleDao.selectPage(new Page<>((offset + limit) /limit, limit), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }

    /**
     * 加载用户菜单
     * @param uid
     * @return
     */
    public List<String> loadUserMenu(Long uid) {
        if (StringUtils.isNull(uid))
            return null;

        List<Map<String, Object>> roleListMap = roleDao.queryUserMenu(uid);
        List<String> roleList = new ArrayList<>();
        for (Map<String, Object> map : roleListMap) {
            if (!StringUtils.isNull(map.get("url")))
                roleList.add(StringUtils.convertString(map.get("url")));
        }

        return roleList;
    }

    /**
     * 加载用户权限列表
     * @param uid
     * @return
     */
    public List<String> loadRoleName(Long uid) {
        if (StringUtils.isNull(uid))
            return null;
        List<Map<String, Object>> roleNameListMap = roleDao.queryRoleName(uid);
        List<String> roleNameList = new ArrayList<>();
        for (Map<String, Object> map : roleNameListMap) {
            if (!StringUtils.isNull(map.get("name")))
                roleNameList.add(StringUtils.convertString(map.get("name")));
        }
        return roleNameList;
    }

    /**
     * 新增数据
     * @param dto page
     * @return
     */
    public Integer saveInit(SysRole dto) {
        if (dto == null)
            return Code.PARAM_ERROR.getCode();

        if (StringUtils.isAllParamNull(dto.getName(), dto.getRoleGroup()))
            return Code.PARAM_ERROR.getCode();

        dto.setStatus(StateEnum.ENABLED.getCode());
        dto.setCreateTime(new Date());

        int saveStatus = roleDao.insert(dto);
        if (saveStatus > 0)
            return 0;
        else
            return -1;

    }

    /**
     * 更新数据状态
     * @param rid 角色ID
     * @param status 状态
     * @return
     */
    public Integer updateStatus(Long rid, Integer status) {
        if (StringUtils.isAllParamNull(rid, status))
            return Code.PARAM_ERROR.getCode();

        SysRole role = new SysRole();
        role.setId(rid);
        role.setStatus(status);

        Integer code = roleDao.updateById(role);
        if (code > 0)
            return 0;
        else
            return -1;
    }

    /**
     * 更新数据
     * @param dto page
     * @return
     */
    public Integer update(SysRole dto) {
        if (dto == null || StringUtils.isNull(dto.getId()))
            return Code.PARAM_ERROR.getCode();

        int saveStatus = roleDao.updateById(dto);
        if (saveStatus > 0) {
            return 0;
        } else {
            return -1;
        }
    }

	@Override
	public List<SysRole> findByName(String name) {
    	QueryWrapper<SysRole> qw = new QueryWrapper<>();
    	qw.eq("status",StateEnum.ENABLED.getCode());
    	if(StringUtils.isNotBlank(name)){
    		qw.eq("name",name);
		}
		List<SysRole> sysRoles = roleDao.selectList(qw);
		return sysRoles;
	}
}
